$authr()


require('../css/index.less')

// 字体
require('../font/iconfont.css')




// 轮播
require('../lib/swiper/swiper-bundle.min.css')

const Swiper = require('../lib/swiper/swiper-bundle.min.js');

const axios = require('../lib/axios/axios.js')




document.ready(() => {

    $footerClick()


    // 轮播
    var mySwiper = new Swiper('.swiper-container', {
        direction: 'horizontal', // 垂直切换选项
        loop: true, // 循环模式选项
        touchRatio: 0.5, //触摸变慢
        longSwipesMs: 5000,
        freeMode: false,
        autoplay: {
            delay: 1500,
            stopOnLastSlide: false,
            disableOnInteraction: false,
        },
        autoHeight: true,
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        }
    })

    // 获取首页的数据
    const rank = document.querySelector('#ranking');
    const badgeCount = document.querySelector('#badgeCount');
    const punchIn = document.querySelector('#punchIn');
    const isCard = document.querySelector('#isCard');

    // 从localStrong获取用户id
    const data = JSON.parse(localStorage.getExpire('data'))
    const uid = data.user.userId;

    // 页面还没有加载完是不能打开的,是有等数据获取后才能打卡
    let resolut = false;

    // 点击打卡
    isCard.addEventListener('click', function () {

        if (resolut) {
            axios.get("http://139.9.177.51:8099/clockIn", {
                params: {
                    userId: uid
                }
            }).then(res => {
                if (res.data.status == 0) {
                    $createMask("icon-dingwei1", "打卡成功!", 1000)
                    getIndexData()
                } else {
                    $createMask("icon-dingwei1", "打卡失败!", 1000)
                }
            }).catch(r => {
                $createMask("icon-dingwei1", "网络繁忙!", 1000)
            })
        } else {
            $createMask("icon-dingwei1", "明日再来!", 1000)
        }

    })

    // 获取首页数据
    getIndexData()

    function getIndexData() {
        axios.get("http://139.9.177.51:8099/headPageInfo", {
            params: {
                userId: uid
            }
        }).then((res) => {
            if (res.data.status != 0) {
                throw "cw"
            }
            // true位打卡了
            JSON.parse(res.data.data.isPunch) ? resolut = false : resolut = true
            renderData(res.data)

        }).catch(reason => {
            console.log(reason);
            $createMask("icon-dingwei1", "当前网络繁忙!", 1000)
        })
    }

    // 渲染数据
    function renderData(res) {
        badgeCount.innerHTML = res.data.insigniaNum
        punchIn.innerHTML = res.data.punchIn
        rank.innerHTML = res.data.rank

        //false 表示没有打卡;
        if (JSON.parse(res.data.isPunch)) {
            isCard.innerHTML = "今日已打卡"
            isCard.classList.add('isCard')
        } else {
            isCard.innerHTML = "今日打卡"
            isCard.classList.remove('isCard')
        }
    }

    // 点击进入课程中心
    let kcCenter = document.querySelector("#kcCenter");
    kcCenter.addEventListener('click', function () {
        location.href = "./projectDes.html"
    })




})